package com.wk1un2.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wk1un2.service.ProductCategoryService;
import com.wk1un2.service.UserService;
import com.wk1un2.util.Result;
import com.wk1un2.service.CarouselService;
import com.wk1un2.entity.ActivitySess;
import com.wk1un2.entity.User;
import com.wk1un2.service.ActivityProductService;
import com.wk1un2.service.ActivitySessService;

/**
 * 商品分类控制器
 * 
 * @author Administrator
 *
 */
@Controller
public class IndexWebController {
	@Autowired
	ProductCategoryService ProductCategoryService;
	@Autowired
	CarouselService CarouselService;
	@Autowired
	ActivitySessService ActivitySessService;
	@Autowired
	UserService UserService;
	@Autowired
	ActivityProductService ActivityProductService;

	@RequestMapping("/Front/login")
	public String login(User user, Model model, HttpSession session) {
		System.out.println(456);
		// 获取用户名和密码
		String u_name = user.getU_name();
		String u_password = user.getU_password();

		User u = UserService.getAll(u_name, u_password);

		if (u != null) {
			// 将用户对象添加到Session中
			session.setAttribute("USER_SESSION", u);
			// 重定向到主页面的跳转方法
			return "redirect:../index";
		}
		// 如果账号密码不正确
		model.addAttribute("msg", "用户名或密码错误，请重新登录！");
		return "forward:/Front/login.jsp";
	}

	/**
	 * 安全退出
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		// 清除session
		session.invalidate();
		// 重定向到首页页面的跳转方法
		return "redirect:index";
	}

	@RequestMapping("/register")
	public String register(@RequestParam Map<String, Object> m) {
		// 使用request获取前台传来的参数
		int i = UserService.add(m);
		if (i>0) {
			return "redirect:index";
			
		}
		return "forward:/login.jsp";		
	}

	@RequestMapping("/index")
	public String proCate(Model model, HttpSession session) {
		List<Map<String, Object>> pc = ProductCategoryService.getAll(0, 9999, null);
		List<Map<String, Object>> carousel = CarouselService.getAll(0, 9999, null);
		List<Map<String, Object>> pcate_icon = ProductCategoryService.getAll(0, 9999, null);
		List<Map<String, Object>> p_icon = ProductCategoryService.getAll(0, 9999, null);
		List<ActivitySess> as = ActivitySessService.getAll(0, 9999, null);

		List<Map<String, Object>> actPS1 = ActivityProductService.getAll(0, 9999, null, 1);
		List<Map<String, Object>> actPS2 = ActivityProductService.getAll(0, 9999, null, 2);
		List<Map<String, Object>> actPS3 = ActivityProductService.getAll(0, 9999, null, 3);
		List<Map<String, Object>> actPS4 = ActivityProductService.getAll(0, 9999, null, 4);
         System.out.println(actPS4);
		model.addAttribute("productcategory", this.getPc(pc));

		model.addAttribute("carousel", carousel);
//		System.out.println(carousel);

		model.addAttribute("pcate_icon", pcate_icon);
//		System.out.println(pcate_icon);
		model.addAttribute("p_icon", p_icon);
//		System.out.println(p_icon);
		model.addAttribute("as", as);

		model.addAttribute("actPS1", actPS1);
		model.addAttribute("actPS2", actPS2);
		model.addAttribute("actPS3", actPS3);
		model.addAttribute("actPS4", actPS4);
		// 重定向到主页面的跳转方法
		System.out.println("--------------------------------------");
		System.out.println(this.getPc(pc));
		System.out.println("--------------------------------------");
		return "index";
	}

	// 分类
	public List<Map<String, Object>> getPc(List<Map<String, Object>> pc) {
		Set set = new HashSet();// 存放的是第一级分类
		for (Map<String, Object> map : pc) {// 循环所有的分类数据
			if (map.get("p_parcate") != null) {
				String lname = map.get("p_parcate").toString();// 上级的分类
				set.add(lname);// set
			}
		}
		List<Map<String, Object>> ls1 = this.getPc2(pc, set);
		return ls1;
	}

//		  
//		  
	public List<Map<String, Object>> getPc2(List<Map<String, Object>> pc, Set set) {
		List<Map<String, Object>> ls = new ArrayList<Map<String, Object>>();// 最终要返回的集合
		Map m = null;// 第一级 -第二级
		List<Map<String, Object>> rTree = null;// 子分类
		for (Object object : set) {
			rTree = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> map : pc) {
				if (map.get("p_parcate") != null) {
					if (object.equals(map.get("p_parcate").toString())) {
						rTree.add(map);// 找到当前父级的子分类
					}
				}
			}
			m = new HashMap();
			for (Map object2 : pc) {
				System.out.println(object2.get("pcat_id").toString());
				System.out.println(object.toString());
				if (object2.get("pcat_id").toString().equals(object.toString())) {
					m.put("tTree", object2.get("pcat_name"));
				}

			}

			m.put("eTree", rTree);
			ls.add(m);
		}
		return ls;
	}
}